<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/21
 * Time: 18:23
 */

namespace app\api\controller;

use app\api\model\WeLikes as LikesModel;
use app\api\model\WeNotice;

class Likes extends Base
{
    public function vote(){
        $topic_id = input('topic_id','0','intval');
        $uid = $this->uid;
        $data['topic_id'] = $topic_id;
        $data['user_id'] = $uid;
        $res = $this->validate($data,'WeLikes');
        if(true!==$res){
            list($this->code,$this->msg) = [0,$res];
            return;
        }
        $result = LikesModel::vote($data);
        if(empty($result)){
            list($this->code,$this->msg) = [0,'点赞失败'];
            return;
        }else{
            if(isset($result['code']) && $result['code'] == 0){
                list($this->code,$this->msg) = [0,$result['msg']];
                return;
            }
            // 消息通知
            $topic_user = \app\api\model\WeTopic::where(['id'=>$topic_id])->value('user_id');
            WeNotice::vote($topic_user,$topic_id);
            list($this->code,$this->msg) = [1,'点赞成功'];
            return;
        }
    }

    public function cancelVote(){
        $topic_id = input('topic_id','0','intval');
        $uid = $this->uid;
        $data['topic_id'] = $topic_id;
        $data['user_id'] = $uid;
        $res = $this->validate($data,'WeLikes');
        if(true!==$res){
            list($this->code,$this->msg) = [0,$res];
            return;
        }
        $res = LikesModel::cancelVote($data);
        if($res){
            if(is_array($res) || isset($res['code']) && $res['code'] == 0){
                list($this->code,$this->msg) = [0,$res['msg']];
                return;
            }
            list($this->code,$this->msg) = [1,'取消点赞成功'];
            return;
        }else{
            list($this->code,$this->msg) = [0,'取消点赞失败'];
            return;
        }
    }
}